<?php

namespace App\Admin\Controllers;

use App\Models\CaseModel;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;

class CaseController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new CaseModel(), function (Grid $grid) {
            $grid->column('id')->sortable();
            $grid->column('content')->limit(10);
            $grid->column('video_url')->display(function (){
                $res = $this->video_url;
                return "<video src = '{$res}' controls='controls' height='50' width='50'";
            });

            $grid->column('status')->switch();
            $grid->filter(function (Grid\Filter $filter) {
                $filter->equal('id');
                $filter->equal('status')->select([0=>'隐藏',1=>'显示']);

            });
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new CaseModel(), function (Show $show) {
            $show->field('id');
            $show->field('content');
            $show->field('status')->using([0=>'隐藏',1=>'显示']);
            $show->field('created_at');
            $show->field('updated_at');
            $show->field('video_url')->unescape()->as(function (){
                $res = $this->video_url;
                return "<video src='{$res}' controls='controls' height='300' width='300'";
            });

        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new CaseModel(), function (Form $form) {
            $form->display('id');
            $form->text('content');
            $form->file('video_url')->accept('mp4');
            $form->radio('status')->options([0=>'隐藏',1=>'显示'])->default(0);

            $form->display('created_at');
            $form->display('updated_at');
            $form->saving(function ($form) {
                $form->video_url = config('APP_URL').'/uploads/'.$form->video_url;

            });
        });
    }
}
